package cn.iocoder.yudao.module.ai.graph.node.customerservice;

import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.action.NodeAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 升级节点 - 将重要问题升级到高级支持团队
 * 
 * @author yudao
 * @since 2025/10/17
 */
public class EscalateNode implements NodeAction {

    private static final Logger logger = LoggerFactory.getLogger(EscalateNode.class);

    @Override
    public Map<String, Object> apply(OverAllState state) {
        logger.info("=== EscalateNode 开始执行 ===");
        
        String customerId = state.value("customer_id", "");
        String query = state.value("customer_query", "");
        String category = state.value("query_category", "");
        String priority = state.value("query_priority", "");
        String sentiment = state.value("query_sentiment", "");
        String humanComments = state.value("human_comments", "");
        
        logger.warn("⚠️  问题升级到高级支持");
        logger.info("客户 ID：{}", customerId);
        logger.info("问题类别：{}", category);
        logger.info("优先级：{}", priority);
        logger.info("客户情绪：{}", sentiment);
        
        if (!humanComments.isEmpty()) {
            logger.info("升级原因：{}", humanComments);
        }
        
        // 确定升级级别
        String escalationLevel = determineEscalationLevel(priority, sentiment, category);
        
        // 创建升级工单
        String ticketId = createEscalationTicket(customerId, query, escalationLevel);
        
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("escalation_status", "success");
        resultMap.put("escalation_level", escalationLevel);
        resultMap.put("ticket_id", ticketId);
        resultMap.put("escalation_reason", humanComments);
        
        logger.info("✓ 已创建升级工单：{}, 级别：{}", ticketId, escalationLevel);
        
        // 发送升级通知
        notifyEscalationTeam(ticketId, escalationLevel, customerId);
        
        return resultMap;
    }

    /**
     * 确定升级级别
     */
    private String determineEscalationLevel(String priority, String sentiment, String category) {
        if ("high".equals(priority) && "negative".equals(sentiment)) {
            return "URGENT"; // 紧急
        } else if ("high".equals(priority) || "negative".equals(sentiment)) {
            return "HIGH"; // 高优先级
        } else if ("投诉建议".equals(category)) {
            return "COMPLAINT"; // 投诉类
        } else {
            return "NORMAL"; // 常规
        }
    }

    /**
     * 创建升级工单
     */
    private String createEscalationTicket(String customerId, String query, String level) {
        // 生成工单 ID
        String ticketId = "ESC-" + System.currentTimeMillis();
        logger.info("📋 创建升级工单 - ID: {}, 级别: {}", ticketId, level);
        // 实际项目中，这里应该调用工单系统 API
        return ticketId;
    }

    /**
     * 通知升级团队
     */
    private void notifyEscalationTeam(String ticketId, String level, String customerId) {
        logger.info("📧 [通知升级团队] 工单：{}, 级别：{}, 客户：{}", ticketId, level, customerId);
        // 实际项目中，这里应该发送邮件或系统通知
    }
}

